package org.example.chapter3_3.service.iml;

import lombok.extern.slf4j.Slf4j;
import org.example.chapter3_3.dao.StudentDao;
import org.example.chapter3_3.entity.Student;
import org.example.chapter3_3.entity.User;
import org.example.chapter3_3.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class StudentServiceIml implements StudentService {
    JdbcTemplate jdbcTemplate;
    public StudentServiceIml(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int addStudent(Student student) {
        String sql = "insert into students (name, age, sex) values (?,?,?)";
        return jdbcTemplate.update(sql,student.getName(),student.getAge(),student.getSex());
    }

    @Override
    public int deleteStudent(int id) {
        String sql = "delete students where id = ?";
        return jdbcTemplate.update(sql,id);
    }

    @Override
    public int updateStudent(Student student) {
        String sql = "update students set name = ?, age = ?, sex = ? where id = ?";
        return jdbcTemplate.update(sql,student.getName(),student.getAge(),student.getSex(),student.getId());
    }

    @Override
    public Student getStudent(int id) {
        String sql = "select * from students where id = ?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Student>(Student.class),id);
    }
}

